package com.decent.ejfadmin.user.dao;

import com.decent.ejfadmin.common.bean.WithdrawBank;
import com.decent.ejfadmin.user.bean.BankCodeMap;
import com.decent.ejfadmin.user.dao.provider.UserWithdrawBankProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * describe:
 *
 * @author JunLei He
 * @date 2018年11月08日
 */
@Repository
@Mapper
public interface UserWithdrawBankDao {

    /**
     * 查询会员提现银行卡列表
     *
     * @param account 会员编号
     * @param state   银行卡状态
     * @return 返回列表
     */
    @SelectProvider(type = UserWithdrawBankProvider.class, method = "queryUserWithdrawBankList")
    List<WithdrawBank> queryUserWithdrawBankList(@Param("account") String account, @Param("state") Integer state);

    /**
     * 修改会员银行卡状态
     *
     * @param bankId 银行卡Id
     * @param state  修改银行卡状态
     * @return 成功修改的行数
     */
    @Update(" UPDATE yuhuang_withdraw_bank " +
            "SET is_use = #{state}, " +
            " gmt_modified = NOW() " +
            "WHERE " +
            " id = #{bankId} ")
    int updateWithdrawBankState(@Param("bankId") Integer bankId, @Param("state") Integer state);


    /**
     * 查询银行卡信息
     *
     * @param id 银行卡编号
     * @return 银行卡信息
     */
    @Select(" SELECT " +
            " b.user_id userId, " +
            " b.name, " +
            " b.bank_telephone bankTelephone " +
            "FROM " +
            " yuhuang_withdraw_bank b " +
            "WHERE " +
            " b.id = #{id} ")
    WithdrawBank queryUserWithdrawBank(@Param("id") Long id);

    /**
     * 添加银行卡信息
     *
     * @param withdrawBankModel 返回
     * @param admin             操作人
     * @return 返回成功条数
     */
    @Insert(" INSERT INTO yuhuang_withdraw_bank ( " +
            " user_id, " +
            " bank_no, " +
            " bank_salt, " +
            " bank_telephone, " +
            " bank_name, " +
            " bank_account_type, " +
            " bank_code, " +
            " NAME, " +
            " admin " +
            ") VALUES " +
            " (#{withdrawBankModel.userId}, #{withdrawBankModel.bankNo}," +
            " #{withdrawBankModel.bankSalt}, #{withdrawBankModel.bankTelephone}, " +
            " #{withdrawBankModel.bankName}, #{withdrawBankModel.bankAccountType},#{withdrawBankModel.bankCode},#{withdrawBankModel.name}, " +
            " #{admin} ) ")
    int addWithdrawBank(@Param("withdrawBankModel") WithdrawBank withdrawBankModel, @Param("admin") String admin);

    /**
     * 查询银行编码
     *
     * @param bankName 银行名称
     * @return
     */
    @Select("SELECT bankCode FROM `yuhuang_bank_code_map` WHERE bankName=#{bankName} and payBank='京东' LIMIT 1")
    String queryBankCode(@Param("bankName") String bankName);

    /**
     * 查询对应银行编码
     *
     * @param bankCodeMap 银行
     * @return
     */
    @SelectProvider(type = UserWithdrawBankProvider.class, method = "queryBankCodeMap")
    List<BankCodeMap> queryBankCodeMap(@Param("bankCodeMap") BankCodeMap bankCodeMap);

    /**
     * 新增银行
     *
     * @param bankCodeMap 银行
     * @return
     */
    @Insert("insert into yuhuang_bank_code_map (bankName,bankAccountType,bankCode,payBank) " +
            "values(#{bankCodeMap.bankName},#{bankCodeMap.bankAccountType},#{bankCodeMap.bankCode},#{bankCodeMap.payBank}) ")
    int insertBankCodeMap(@Param("bankCodeMap") BankCodeMap bankCodeMap);

    /**
     * 更新会员提现银行卡的编码
     *
     * @param bankCodeMap 银行
     * @return
     */
    @Update("update yuhuang_withdraw_bank set bank_code=#{bankCodeMap.bankCode} where bank_name=#{bankCodeMap.bankName}")
    int updateWithDrawBank(@Param("bankCodeMap") BankCodeMap bankCodeMap);

    /**
     * 新增银行
     *
     * @param bankCodeMap 银行
     * @return
     */
    @Update("update yuhuang_bank_code_map set bankName=#{bankCodeMap.bankName}," +
            "bankAccountType=#{bankCodeMap.bankAccountType},bankCode=#{bankCodeMap.bankCode} where id=#{bankCodeMap.id}")
    int updateBankCodeMap(@Param("bankCodeMap") BankCodeMap bankCodeMap);
}
